<?php

declare(strict_types=1);

namespace app\api\model;

use app\api\model\Week as ModelWeek;
use app\api\model\Course as ModelCourse;
use app\api\model\Tool as ModelTool;

/**
 * 阶段模型
 */
class Stage extends Base
{
    protected $autoWriteTimestamp = 'datetime';


    /**
     * # 获取课程一阶段周期
     * @param string $id
     * @param string $stage_name
     * @return array
     */
    public static function getFirstStageWeek($id, $stage_name)
    {
        $course = ModelCourse::getCourse($id);
        if (empty($course->toArray())) {
            return false;
        }
        $stage = stage::getStage($course->id);
        if (empty($stage->toArray())) {
            return 0;
        }
        $arrStage = $stage->toArray();
        stage::getStageName($stage);
        if (!isset($arrStage[$stage_name - 1]['stage']) || $arrStage[$stage_name - 1]['stage'] != $stage_name) {
            return 0;
        };
        $stage_id = $arrStage[$stage_name - 1];
        $week = ModelWeek::getWeek($stage_id['id']);
        if (empty($week->toArray())) {
            return 0;
        }
        $allCourse = ModelTool::mergeArray($arrStage, $week->toArray(), 'stage_id', 'weekCouser');
        return $allCourse;
    }
    
    /**
     * # 获取单个阶段下课程
     * @param string $stage_id
     * @return object
     */

    public static function getOnlyCourse($stage_id)
    {
        $data = ModelWeek::getWeek($stage_id);
        return $data;
    }

    /**
     * # 获取课程下阶段
     * @param string $course_id
     * @param string $order
     * @param string $method
     * @return object
     */
    public static function getStage($course_id, $order = 'asc', $method = 'select')
    {
        $stage = Stage::where('course_id', $course_id)->field('id,stage')->order('stage', $order)->$method();
        return $stage;
    }

    /**
     * # 新增阶段
     * @param string $course_id 
     * @param string $user_id
     * @return object
     */

    public static function addStage($course_id, $user_id)
    {
        $stage = Stage::getStage($course_id, 'desc', 'find');
        if (empty($stage)) {
            $stage_name = 0;
        } else {
            $stage_name = $stage->stage;
        }
        $data = [
            'course_id' => $course_id,
            'user_id' => $user_id,
            'stage' => ++$stage_name,
        ];
        $success = stage::create($data);
        return $success;
    }

    /**
     * # 获取阶段名称
     * @param object
     */
    public static function getStageName(&$data)
    {
        $arr1 = ['', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
        $arr2 = ['', '阶段十', '阶段二十', '阶段三十', '阶段四十', '阶段五十', '阶段六十', '阶段七十', '阶段八十', '阶段九十'];
        if (isset($data->stage)) {
            if ($data->stage <= 9) {
                $data->stage = '阶段' . $arr1[$data->stage];
            } else {
                $str = (string)$data->stage;
                $data->stage = $arr2[$str[0]] . $arr1[$str[1]];
            }
        } else {
            foreach ($data as $key => $el) {
                $stageName = '';
                if ($el['stage'] <= 9) {
                    $stageName = '阶段' . $arr1[$el['stage']];
                } else {
                    $str = (string)$el['stage'];
                    $stageName = $arr2[$str[0]] . $arr1[$str[1]];
                }
                $data[$key]['stageName'] = $stageName;
            }
        }
    }
}
